package cn.apestar.common.chat.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.apestar.common.chat.Chat;
import cn.apestar.common.chat.ChatInstance;
import cn.apestar.common.chat.enums.AppType;
import cn.apestar.common.chat.enums.MessageType;
import cn.apestar.common.chat.enums.MessageWTType;
import cn.apestar.common.huanxin.PeoPleType;

/**
 * 消息发送
 * 
 * @author lvh
 */
public class Message implements Runnable {

    String userIds;

    String type;

    String message;

    String messageType;

    String fromOne;

    String appType = AppType.NTO.getValue();

    List list = new ArrayList();

    public Message() {

    }

    public Message(String appType) {
        this.appType = appType;
    }

    public Message(String userIds, String message, String messageType, String type, String fromOne, Object... args) {
        this.userIds = userIds;
        this.type = type;
        this.message = message;
        this.messageType = messageType;
        this.fromOne = fromOne;
        for (int i = 1; i < args.length; i += 2) {
            Map map = new HashMap();
            map.put("code", String.valueOf(args[i - 1]));
            map.put("value", args[i]);
            list.add(map);
        }
    }

    /**
     * 初始化消息类
     * 
     * @param userIds
     *            用户类型
     * @param message
     *            消息类型
     * @param args
     *            参数
     */
    public Message(String userIds, String message, Object... args) {
        this.userIds = userIds;
        this.message = message;
        for (int i = 1; i < args.length; i += 2) {
            Map map = new HashMap();
            map.put("code", String.valueOf(args[i - 1]));
            map.put("value", args[i]);
            list.add(map);
        }
    }

    /**
     * 初始化消息类
     * 
     * @param userIds
     *            用户类型
     * @param message
     *            消息类型
     * @param formOne
     *            谁发的
     */
    public Message(String appType, String userIds, String message, String formOne, Object... args) {
        this.userIds = userIds;
        this.message = message;
        this.fromOne = formOne;
        this.appType = appType;
        for (int i = 1; i < args.length; i += 2) {
            Map map = new HashMap();
            map.put("code", String.valueOf(args[i - 1]));
            map.put("value", args[i]);
            list.add(map);
        }
    }

    /**
     * 发送消息
     * 
     * @author lvh 2016年6月16日 下午2:55:47</br>
     * @return void
     */
    public void sendMessage() {

        this.messageType = MessageType.TXT.getValue();
        this.run();
    }

    /**
     * 发送系统消息
     * 
     * @author lvh 2016年6月16日 下午2:55:47</br>
     * @return void
     */
    public void sendSystemMessage() {

        this.type = MessageWTType.PT.getValue();
        this.messageType = MessageType.TXT.getValue();
        this.fromOne = PeoPleType.SYSTEM.getValue();
        this.run();
    }

    /**
     * 待处理事项通知信息
     * 
     * @author lvh 2016年6月16日 下午2:55:47</br>
     * @return void
     */
    public void sendPendingMattersMessage() {

        this.type = MessageWTType.PENDING_MATTERS.getValue();
        this.messageType = MessageType.TXT.getValue();
        this.fromOne = MessageWTType.PENDING_MATTERS.getValue();
        this.run();
    }

    /**
     * 发送简历状态消息
     * 
     * @author lvh 2016年6月16日 下午2:55:47</br>
     * @return void
     */
    public void sendResumeStateMessage() {

        this.type = MessageWTType.RESUME_STATE.getValue();
        this.messageType = MessageType.TXT.getValue();
        this.fromOne = MessageWTType.RESUME_STATE.getValue();
        this.run();
    }

    /**
     * 发送谁看过我消息
     * 
     * @author lvh 2016年6月16日 下午2:55:47</br>
     * @return void
     */
    public void sendSeenmeMessage() {

        this.type = MessageWTType.SEENME.getValue();
        this.messageType = MessageType.TXT.getValue();
        this.fromOne = MessageWTType.SEENME.getValue();
        this.run();
    }

    public String getUserIds() {

        return userIds;
    }

    public void setUserIds(String userIds) {

        this.userIds = userIds;
    }

    public String getType() {

        return type;
    }

    public void setType(String type) {

        this.type = type;
    }

    public String getMessage() {

        return message;
    }

    public void setMessage(String message) {

        this.message = message;
    }

    public String getMessageType() {

        return messageType;
    }

    public void setMessageType(String messageType) {

        this.messageType = messageType;
    }

    @Override
    public void run() {

        Chat chat = ChatInstance.getChat(appType);
        // TODO Auto-generated method stub
        String[] s1 = userIds.split(",");
        if (s1.length < 20) {
            chat.transmissionMessageFromOneToOne(fromOne, userIds, message, messageType, type, list);
        } else {
            int m = s1.length;
            int n = 0;
            List lists = Arrays.asList(s1);
            while (n < m) {
                int max = n + 20;
                if (max > m) {
                    max = m;
                }
                List listTrans = lists.subList(n, max);
                n = n + 20;
                String s = "'1'";
                for (int i = 0; i < listTrans.size(); i++) {
                    Map r2 = (Map) listTrans.get(i);
                    s += "," + r2.get("id");
                }
                chat.transmissionMessageFromOneToOne(fromOne, s, message, messageType, type, list);
            }
        }
    }

}
